{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(0, 5), match='hello'>\n",
      "<re.Match object; span=(0, 10), match='2022-03-10'>\n",
      "<re.Match object; span=(2, 12), match='2022-03-10'>\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "# 匹配的字符串以hello开头\n",
    "str_1 = re.compile(r'^hello')\n",
    "print(str_1.match('hello'))\n",
    "\n",
    "# 分组匹配\n",
    "str_2 = re.compile(r'(\\d+)-(\\d+)-(\\d+)')\n",
    "print(str_2.match('2022-03-10bb'))\n",
    "\n",
    "print(str_2.search('aa2022-03-10bb'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022 03 11\n",
      "aaaa-aa-aa\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "# 替换字符串\n",
    "\n",
    "# 把 - 替换成 “ ”\n",
    "print(re.sub('-',' ','2022-03-11'))\n",
    "# 把数字替换成 a\n",
    "print(re.sub('\\d','a','2022-03-11'))"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "ff79ebfcf6806606a412477be2872c3ec52671763ecb054ca3010c66083164a3"
  },
  "kernelspec": {
   "display_name": "Python 3.8.3 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
